package com.yang.contoller;

import com.alibaba.excel.EasyExcel;
import com.csvreader.CsvReader;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.yang.config.CostDataListener;
import com.yang.config.DetailsDataListener;
import com.yang.entity.Cost;
import com.yang.entity.Details;
import com.yang.service.CostService;
import com.yang.service.DetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yds
 * @program spring-security
 * @description
 * @create 2020-11-30 10:50
 **/
@Controller
public class RouterContoller {

    @Autowired
    public CostService costService;
    @Autowired
    public DetailsService detailsService;

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }

    // easyexcel读取文件
    @GetMapping("/readCost")
    @ResponseBody
    public String readCost() {
        String fileName = "C:\\Users\\93461\\Desktop\\杨代星的记件\\" + "1.xlsx";
        EasyExcel.read(fileName, Cost.class, new CostDataListener(costService)).sheet().doRead();
        return "读取成功";
    }
    @GetMapping("/readDetails")
    @ResponseBody
    public String readDetails() throws IOException {
        String fileName = "C:\\Users\\93461\\Desktop\\杨代星的记件\\" + "2.csv";
       // EasyExcel.read(fileName, Details.class, new DetailsDataListener(detailsService)).sheet().doRead();
        CsvReader csvReader = new CsvReader(fileName, ',', Charset.forName("GBK"));

        // 如果你的文件没有表头，这行不用执行
        // 这行不要是为了从表头的下一行读，也就是过滤表头
        csvReader.readHeaders();
        List<Details> list=new ArrayList<>();
        // 读取每行的内容
        while (csvReader.readRecord()) {
            // 获取内容的两种方式
            // 1. 通过下标获取
            Details model=new Details();
            model.setSendNo(csvReader.get(1));
            model.setTime(csvReader.get(3));
            model.setOwer(csvReader.get(6));
            model.setToWhere(csvReader.get(10));
            model.setWeight(Double.parseDouble(csvReader.get(12)));
            model.setPrice(Double.parseDouble(csvReader.get(23)));
            list.add(model);
        }
        detailsService.saveBatch(list);
        return "读取成功";
    }



}
